
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/incubator-trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	"log"
	"time"
)

type DeliveryservicesUsers struct {
	Deliveryservice string                     `db:"deliveryservice" json:"deliveryservice"`
	Username        string                     `db:"username" json:"username"`
	CreatedAt       time.Time                  `db:"created_at" json:"createdAt"`
	Links           DeliveryservicesUsersLinks `json:"_links" db:-`
}

type DeliveryservicesUsersLinks struct {
	Self string `db:"self" json:"_self"`
}

// @Title getDeliveryservicesUsersById
// @Description retrieves the deliveryservices_users information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    DeliveryservicesUsers
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_users/{id} [get]
func getDeliveryservicesUser(deliveryservice string, username string, db *sqlx.DB) (interface{}, error) {
	ret := []DeliveryservicesUsers{}
	arg := DeliveryservicesUsers{}
	arg.Deliveryservice = deliveryservice
	arg.Username = username
	queryStr := "select *, concat('" + API_PATH + "deliveryservices_users', '/deliveryservice/', deliveryservice, '/username/', username) as self"
	queryStr += " from deliveryservices_users WHERE deliveryservice=:deliveryservice AND username=:username"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getDeliveryservicesUserss
// @Description retrieves the deliveryservices_users
// @Accept  application/json
// @Success 200 {array}    DeliveryservicesUsers
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_users [get]
func getDeliveryservicesUsers(db *sqlx.DB) (interface{}, error) {
	ret := []DeliveryservicesUsers{}
	queryStr := "select *, concat('" + API_PATH + "deliveryservices_users', '/deliveryservice/', deliveryservice, '/username/', username) as self"
	queryStr += " from deliveryservices_users"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postDeliveryservicesUsers
// @Description enter a new deliveryservices_users
// @Accept  application/json
// @Param                 Body body     DeliveryservicesUsers   true "DeliveryservicesUsers object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_users [post]
func postDeliveryservicesUser(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v DeliveryservicesUsers
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO deliveryservices_users("
	sqlString += "deliveryservice"
	sqlString += ",username"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":deliveryservice"
	sqlString += ",:username"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putDeliveryservicesUsers
// @Description modify an existing deliveryservices_usersentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     DeliveryservicesUsers   true "DeliveryservicesUsers object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_users/{id}  [put]
func putDeliveryservicesUser(deliveryservice string, username string, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg DeliveryservicesUsers
	err := json.Unmarshal(payload, &arg)
	arg.Deliveryservice = deliveryservice
	arg.Username = username
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE deliveryservices_users SET "
	sqlString += "deliveryservice = :deliveryservice"
	sqlString += ",username = :username"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE deliveryservice=:deliveryservice AND username=:username"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delDeliveryservicesUsersById
// @Description deletes deliveryservices_users information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    DeliveryservicesUsers
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_users/{id} [delete]
func delDeliveryservicesUser(deliveryservice string, username string, db *sqlx.DB) (interface{}, error) {
	arg := DeliveryservicesUsers{}
	arg.Deliveryservice = deliveryservice
	arg.Username = username
	result, err := db.NamedExec("DELETE FROM deliveryservices_users WHERE deliveryservice=:deliveryservice AND username=:username", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
